﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Groupware;

namespace GUI
{
    public partial class tripDetailDriverView : TripLOA.GUI.internationalization.LOABasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            /*if (Request.QueryString != null & Request.QueryString[0] != null)
            {
                try
                {
                    id = int.Parse(Request.QueryString[0]);
                }
                catch (FormatException exc)
                {
                    ///TODO: treat Exception for invalid querystring in driverTripDetail
                }
            }
            else
            {
                Response.Redirect("~/secure/Home.aspx", false);
            }*/
            if (Request.QueryString != null & Request.QueryString.Count>0)
            {
                try
                {
                    id = int.Parse(Request.QueryString[0]);
                }
                catch (FormatException exc)
                {
                    ///TODO: treat Exception for invalid querystring in driverTripDetail
                }
                //Groupware.Trip trip = Groupware.TripManager.GetTripById(id);
            }
            else
            {
                ///mettere redirect
            }
            if (!IsPostBack)
            {
                //detailMultiView.ActiveViewIndex = 0;
                Groupware.Trip trip = Groupware.TripManager.GetTripById(535);
                Storage.EntityModels.Trip dbtrip = Groupware.TripManager.GetDbTripById(535);
                GeoRef.GeoTrip gt = new GeoRef.GeoTrip(dbtrip);
               
                map_1.EditableTrip = gt;
                //loadLabel(trip);
                loadUsers(trip);
            }
        }

        /*private void loadLabel(Groupware.Trip trip)
        {
            // GW: abbiamo per adesso inserito una strina in attesa che storage ci metta a disposizione Departure e Arrival nell'oggetto Trip
            Groupware.Place stages = trip.GetArrival();
            
            arrivalLocation.Text = stages.Address + ", " + stages.City + ", " + stages.State;

            stages = trip.GetDeparture();
            startLocation.Text = stages.Address + ", " + stages.City + ", " + stages.State;
            
            startTime.Text = trip.DepartureDate.Value.Date+ " at " + trip.DepartureDate.Value.TimeOfDay;
            seatNumber.Text = "" + trip.FreeSeats;

            HourTextBox.Text = trip.DepartureDate.Value.TimeOfDay.Hours.ToString();
            MinuteTextBox.Text = trip.DepartureDate.Value.TimeOfDay.Minutes.ToString();
            SeatTextBox.Text = "" + trip.FreeSeats;
            if (trip.RequireLicenced != null)
                reqLicence.Text = trip.RequireLicenced.Value.ToString();
            if(trip.ReservationExpirationDate!=null)
                reservationExpiration.Text = ""+trip.ReservationExpirationDate.Value;
            if(trip.EstimatedTime!=null)
                estimatedTime.Text = trip.EstimatedTime.Value + " minutes";
            if(trip.EstimatedBudget!=null)
                estimatedBudget.Text = ""+trip.EstimatedBudget.Value;
            if(trip.DeviationRadius!=null)
                deviationRadius.Text = trip.DeviationRadius.Value + " km";
            if(trip.AcceptSmoker!=null)
                if (trip.AcceptSmoker.Value)
                    acceptSmokers.Text = "Yes";
                else
                    acceptSmokers.Text = "No";
            if(trip.Note!=null)
            generalNotes.Text = trip.Note.Text;
            /*departureNotes.Text = ""; // trip.DepartureNote;
            arrivalNotes.Text = ""; // trip.ArrivalNote;*/
        //}

        private void loadUsers(Groupware.Trip trip)
        {
            System.Collections.Generic.List<Security.Profile> aUser =Groupware.TripManager.GetAssociatedUsers(trip.Id);
            for (int i = 0; i < aUser.Count; i++)
            {
                requestingCheckBoxList.Items.Add(aUser[i].Username); 
            }
            aUser = Groupware.TripManager.GetReservatedUsers(trip.Id);
            for (int i = 0; i < aUser.Count; i++)
            {
                acceptedCheckBoxList.Items.Add(aUser[i].Username);
            }
        }

        /*protected void startModifyButton_Click(object sender, EventArgs e)
        {
            detailMultiView.ActiveViewIndex = 1;
        }

        protected void cancelModButton_Click(object sender, EventArgs e)
        {
            detailMultiView.ActiveViewIndex = 0;
        }

        protected void confirmModButton_Click(object sender, EventArgs e)
        {
            int id = 0;
            if (Request.QueryString != null & Request.QueryString[0] != null)
            {
                try
                {
                    id = int.Parse(Request.QueryString[0]);
                }
                catch (FormatException)
                {
                    ///TODO: treat Exception for invalid querystring in driverTripDetail
                    Response.Redirect("~/secure/Home.aspx", false);
                }
            }
            else
            {
                Response.Redirect("~/secure/Home.aspx", false);
            }
            Groupware.Trip trip = TripManager.GetTripById(id);

            saveMod(trip);

            TripManager.Store(trip);

            loadLabel(trip);
            detailMultiView.ActiveViewIndex = 0;
        }

        private void saveMod(Groupware.Trip trip)
        {

        }
        */
        protected void rejectUserButton_Click(object sender, EventArgs e)
        {
            while (requestingCheckBoxList.SelectedItem != null)
                requestingCheckBoxList.Items.Remove(requestingCheckBoxList.SelectedItem);
        }

        protected void acceptUserButton_Click(object sender, EventArgs e)
        {
            while (requestingCheckBoxList.SelectedItem != null)
            {
                if (acceptedCheckBoxList.Items.Count < 20)
                {
                    acceptedCheckBoxList.Items.Add(requestingCheckBoxList.SelectedItem);
                    requestingCheckBoxList.Items.Remove(requestingCheckBoxList.SelectedItem);
                }
            }
        }

        protected void cancelAcceptedUsersButton_Click(object sender, EventArgs e)
        {
            while (acceptedCheckBoxList.SelectedItem != null)
            {
                requestingCheckBoxList.Items.Add(acceptedCheckBoxList.SelectedItem);
                acceptedCheckBoxList.Items.Remove(acceptedCheckBoxList.SelectedItem);
            }
        }
    }
}
